package com.sskj.fabi.ui.fragment;

import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.flyco.tablayout.SegmentTabLayout;
import com.flyco.tablayout.SlidingTabLayout;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.sskj.common.adapter.MyFragmentPagerAdapter;
import com.sskj.common.base.App;
import com.sskj.common.util.ClickUtil;
import com.sskj.common.util.ToastUtil;
import com.sskj.fabi.R2;
import com.sskj.fabi.bean.PayWayBean;
import com.sskj.fabi.component.DaggerUserDataComponent;
import com.sskj.fabi.presenter.FaBiPresenter;
import com.sskj.fabi.R;
import com.sskj.fabi.R2;
import com.sskj.fabi.util.UserChecked;
import com.sskj.lib.RConfig;
import com.sskj.lib.base.BaseFragment;
import com.sskj.lib.bean.UserData;
import com.sskj.lib.model.room.UserViewModel;
import com.sskj.lib.util.AppOuterCheckUtil;
import com.sskj.lib.util.TipUtil;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import io.reactivex.Flowable;

/**
 * DESC: (类描述)法币交易--最开始Fragment
 */
@Route(path = RConfig.FABI_FRAGMENT)//法币Fragment
public class FaBiFragment extends BaseFragment<FaBiPresenter> {

    @BindView(R2.id.slidingTabLayout)
    SlidingTabLayout slidingTabLayout;
    @BindView(R2.id.fabi_trading_hall_viewPager)
    ViewPager fabiTradingHallViewPager;
    @Inject
    UserViewModel userViewModel;
    private UserData userData;
    private List<String> titles = new ArrayList<>();
    private List<Fragment> fragmentList = new ArrayList<>();

    @Override
    protected int getLayoutId() {
        return R.layout.fabi_fragment_fa_bi;
    }

    @Override
    protected FaBiPresenter getPresenter() {
        return new FaBiPresenter();
    }

    @Override
    public void initView() {
        super.initView();
        DaggerUserDataComponent.create().inject(this);
        userViewModel.getUsers().observe(this, users -> {
            if (users != null && users.size() > 0) {
                userData = users.get(0);
            } else {
                userData = null;
            }
        });
        userViewModel.update();
        titles.add(App.INSTANCE.getString(R.string.fabiTradingHallFragment0));  //购买
        titles.add(App.INSTANCE.getString(R.string.fabiTradingHallFragment1));   // 出售
        titles.add(App.INSTANCE.getString(R.string.fabiTradingHallFragment3));   // 记录
        fragmentList.add(TradingHallBuyFragment.newInstance(true)); //购买Fragment
        fragmentList.add(TradingHallBuyFragment.newInstance(false)); // 出售Fragment
        fragmentList.add(RecordFragment.newInstance());  // 记录Fragment
        fabiTradingHallViewPager.setAdapter(new MyFragmentPagerAdapter(getChildFragmentManager(), titles, fragmentList));
        fabiTradingHallViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                if (position == 2) {
                    if (userData == null) {  // 登录状态
                        fabiTradingHallViewPager.setCurrentItem(1);
                        ToastUtil.showShort(App.INSTANCE.getString(R.string.fabiTradingHallBuyFragment7));
                        ARouter.getInstance().build(RConfig.MINE_LOGIN).navigation();
                        return;
                    }
                }
            }

            @Override
            public void onPageSelected(int position) {

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        slidingTabLayout.setViewPager(fabiTradingHallViewPager);
        slidingTabLayout.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                if (position == 2) {
                    if (userData == null) {  // 登录状态
                        slidingTabLayout.setCurrentTab(0);
                        ToastUtil.showShort(App.INSTANCE.getString(R.string.fabiTradingHallBuyFragment7));
                        ARouter.getInstance().build(RConfig.MINE_LOGIN).navigation();
                        return;
                    }
                }
            }

            @Override
            public void onTabReselect(int position) {

            }
        });
    }
}
